package com.example.iotmanagerment26.mapper;

import com.example.iotmanagerment26.domain.Device;
import com.example.iotmanagerment26.domain.DeviceHold;
import com.example.iotmanagerment26.domain.vo.DeviceHoldVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface DeviceHoldMapper {
    @Select("select * from devicehold2 where userName=#{userName} and devName=#{devName}")
    DeviceHold selectHoldByUserAndDevice(DeviceHold dh);

    @Update("update devicehold2 set holded=#{holded} where userName=#{userName} and devName=#{devName}")
    int updateDeviceHold(DeviceHold dh);

    @Insert("insert into devicehold2 values(null,#{userName},#{devName},1)")
    int insertDeviceHold(DeviceHold dh);

    @Select("select * from device where name not in(select devName from devicehold2 where userName=#{username} and holded=1)")
    List<Device> selectUnbindDeviceByUser(String username);

    @Select("select * from devicehold2 where holded=1")
    @Results({
            @Result(
                    property = "user",   //实体类DeviceHoldVO中的user字段 传给下面
                    column = "userName",  //selectUserByName方法的userName字段
                    one = @One(select = "com.example.iotmanagerment26.mapper.UserMapper.selectUserByName")
            ),
            @Result(
                    property = "device",
                    column = "devName",
                    one = @One(select = "com.example.iotmanagerment26.mapper.DeviceMapper.selectDeviceByName")
            )
    })
    List<DeviceHoldVO> selectAllDeviceHoldList();

    @Select("select * from device where name in(select devName from devicehold2 where userName=#{username} and holded=1)")
    List<Device> selectbindDeviceByUser(String username);
}
